package com.tuanproject.controller;

import java.sql.SQLException;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.tuanproject.business.EventsBusiness;
import com.tuanproject.model.Events;
import com.tuanproject.util.SendMailUtil;

/**
 * 
 * @author TuanLe
 * 
 */
@RequestMapping("/")
@Controller
@SessionAttributes({ "methods", "patterns" })
public class HomeController {

	@Autowired
	EventsBusiness eventsBusiness;
	@Autowired
	SendMailUtil sendMailUtil;

	@RequestMapping(value = "")
	public String index(Model ui) {
		try {
			List<Events> list;
			list = eventsBusiness.getAllEvents();
			List<String> methods = new ArrayList<String>();
			methods.add("");
			methods.add("GET");
			methods.add("POST");

			List<String> patterns = new ArrayList<String>();
			patterns.add("");
			patterns.add("lfi");
			patterns.add("rfi");
			patterns.add("unknown");

			ui.addAttribute("methods", methods);
			ui.addAttribute("patterns", patterns);
			ui.addAttribute("ip", "");
			ui.addAttribute("port", "");
			ui.addAttribute("method", "");
			ui.addAttribute("fromDate", "");
			ui.addAttribute("toDate", "");
			ui.addAttribute("listEvents", list);
			ui.addAttribute("listSize", list.size());

			Thread thread = new Thread(new Runnable() {

				@Override
				public void run() {
					while (true) {
						Date date = new Date();
						int year = Calendar.getInstance().get(Calendar.YEAR);
						String month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) + "" : "0" + (date.getMonth() + 1);
						String day = date.getDate() > 9 ? date.getDate() + "" : "0" + date.getDate();
						String toDate = year + "-"
								+ month + "-" + day
								+ " " + date.getHours() + ":"
								+ date.getMinutes() + ":" + date.getSeconds();
						date.setTime(date.getTime() - 50000);
						String fromDate = year + "-"
								+ month + "-" + day
								+ " " + date.getHours() + ":"
								+ date.getMinutes() + ":" + date.getSeconds();
						try {
							List<Events> list = eventsBusiness.getEventsByTime(fromDate, toDate);
							if (list.size() > 0) {
								Map<String, Object> model = new HashMap<String, Object>();
								model.put("listEvents", list);
								model.put("listSize", list.size());
								sendMailUtil.sendMail("Dear Mr. Tuan", model);								
							}
							Thread.sleep(50000);
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			});
			thread.start();

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "index";
	}
}
